package com.hyh.eblog.controller;

import com.hyh.eblog.common.lang.Result;
import com.hyh.eblog.entity.Post;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.FileReader;

/**
 * @author Summerday
 */
@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController{

    /**
     * 管理员的set操作
     * @param id
     * @param rank 0标识取消 1标识操作
     * @param field
     * @return
     */
    @ResponseBody
    @PostMapping("/set")
    public Result set(Long id,Integer rank,String field){

        Post post = postService.getById(id);
        Assert.notNull(post,"该帖子已被删除");
        if("delete".equals(field)){
            postService.removeById(id);
            return Result.success();
        }else if("status".equals(field)){
            post.setRecommend(rank == 1);
        }else if("stick".equals(field)){
            post.setLevel(rank);
        }
        postService.updateById(post);
        return Result.success();
    }

}
